using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PocketLibrary.Image;

namespace PocketApps
{
    public partial class flickrPhotoSetMgr : Form
    {

        #region private variables
        bool _titleLabel = true;
        string _titleString = "Add photoset title...";

        bool _descLabel = true;
        string _descString = "Add optional description...";

        enum PhotoSetManagerState
        {
            BrowsingSets,
            AddingNew,
            Editing,
            Removing,

        }

        PhotoSetManagerState State = PhotoSetManagerState.BrowsingSets;


        PocketSettings.PocketSettings settings = PocketSettings.PocketSettings.Create();

        flickr myFlickr = new flickr();
        #endregion

        public flickrPhotoSetMgr()
        {
            InitializeComponent();
        }

        private void flickrPhotoSetMgr_Load(object sender, EventArgs e)
        {
            myFlickr = new flickr(settings.SelectedUserProfile.Settings.Get("flickrToken"));
            myFlickr.OnResponse += new flickrResponseHandler(myFlickr_OnResponse);
            myFlickr.OnPhotoListResponse += new flickrResponseHandler(myFlickr_OnPhotoListResponse);
            myFlickr.OnPhotoSetListResponse += new flickrResponseHandler(myFlickr_OnPhotoSetListResponse);
            myFlickr.OnErrorResponse += new flickrErrorHandler(myFlickr_OnErrorResponse);
            //myFlickr.GetPhotoSets("");
            this.Focus();
        }

        void myFlickr_OnResponse(object sender, EventArgs args)
        {
            //throw new Exception("The method or operation is not implemented.");
            myFlickr.GetPhotoSets("");
        }

        void myFlickr_OnErrorResponse(object sender, FlickrErrorArgs args)
        {
            MessageBox.Show(args.ErrorMessage, 
                "flickr api error", 
                MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button1);
        }

        void myFlickr_OnPhotoSetListResponse(object sender, EventArgs args)
        {
            lvPhotoSets.Invoke(new EventHandler(delegate
            {
                lvPhotoSets.Items.Clear();
                for (int i = 0; i < myFlickr.PhotoSets.Count; i++)
                {
                    //lbPhotosets.Items.Add(myFlickr.PhotoSets[i].Title);
                    ListViewItem flickrItem = new ListViewItem();
                    flickrItem.Text = myFlickr.PhotoSets[i].Title;
                    flickrItem.SubItems.Add(myFlickr.PhotoSets[i].PhotoCount.ToString());
                    flickrItem.SubItems.Add(myFlickr.PhotoSets[i].Id);
                    flickrItem.SubItems.Add(myFlickr.PhotoSets[i].Description);

                    // add photosets to listview
                    lvPhotoSets.Items.Add(flickrItem);
                }
                //lvFlickr.Items.Add(new ListViewItem(
                //    new string[] {
                //            "photos not in set", // title
                //            "n/a",  // photo count
                //            "0", // id
                //            "Photos not assigned to any sets" // description
                //        }));
            }));
        }

        void myFlickr_OnPhotoListResponse(object sender, EventArgs args)
        {
            lvPhotos.Invoke(new EventHandler(delegate
            {
                lvPhotos.Items.Clear();
                for (int i = 0; i < myFlickr.CurrentPhotos.Count; i++)
                {
                    flickrListViewItem photoitem = new flickrListViewItem();
                    photoitem.Text = myFlickr.CurrentPhotos[i].Title;
                    photoitem.SubItems.Add(myFlickr.CurrentPhotos[i].Id);
                    photoitem.SubItems.Add(myFlickr.CurrentPhotos[i].ThumbnailUrl);
                    photoitem.PhotoData = myFlickr.CurrentPhotos[i];
                    lvPhotos.Items.Add(photoitem);
                }
            }));
        }

        private void miClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void tbTitle_GotFocus(object sender, EventArgs e)
        {
            if (_titleLabel)
            {
                this.tbTitle.ForeColor = SystemColors.WindowText;
                this.tbTitle.Text = "";
                _titleLabel = false;
            }
        }

        private void tbTitle_LostFocus(object sender, EventArgs e)
        {
            if (this.tbTitle.Text == "")
            {
                this.tbTitle.Text = _titleString;
                this.tbTitle.ForeColor = SystemColors.GrayText;
                _titleLabel = true;
            }
        }


        private void tbDescription_GotFocus(object sender, EventArgs e)
        {
            if (_descLabel)
            {
                this.tbDescription.ForeColor = SystemColors.WindowText;
                this.tbDescription.Text = "";
                _descLabel = false;
            }
        }

        private void tbDescription_LostFocus(object sender, EventArgs e)
        {
            if (this.tbDescription.Text == "")
            {
                this.tbDescription.Text = _descString;
                this.tbDescription.ForeColor = SystemColors.GrayText;
                _descLabel = true;
            }
        }

        private void miNewPhotoset_Click(object sender, EventArgs e)
        {
            this.SuspendLayout();
            switch (State)
            {
                case PhotoSetManagerState.BrowsingSets:
                    State = PhotoSetManagerState.AddingNew;
                    pnPhotosetList.Visible = !pnPhotosetList.Visible;
                    pnNewSet.Visible = !pnNewSet.Visible;
                    myFlickr.GetPhotosNotInSet();
                    //myFlickr.GetPeoplePublicPhotos(myFlickr.NSID);
                    miNewPhotoset.Text = "Ok";

                    miCancel.Enabled = true;
                    miDeletePhotoset.Enabled = false;
                    miEditPhotoset.Enabled = false;
                    tbTitle_LostFocus(this, e);
                    tbDescription_LostFocus(this, e);

                    break;
                case PhotoSetManagerState.AddingNew:
                    string primaryPhoto ="-1";
                    for (int i = 0; i < lvPhotos.Items.Count; i++)
                    {
                        if (lvPhotos.Items[i].Checked)
                        {
                            flickrListViewItem item = (flickrListViewItem)lvPhotos.Items[i];
                            primaryPhoto = item.PhotoData.Id;
                            break;
                        }
                    }
                    if (primaryPhoto != "-1")
                    {
                        State = PhotoSetManagerState.BrowsingSets;
                        pnNewSet.Visible = !pnNewSet.Visible;
                        pnPhotosetList.Visible = !pnPhotosetList.Visible;
                        miNewPhotoset.Text = "New";

                        flickrPhotoset newset =
                            new flickrPhotoset(tbTitle.Text,
                            tbDescription.Text,
                            primaryPhoto);
                        newset.Title = newset.Title.Replace(" ", "%20");

                        if (_descLabel)
                            newset.Description = "";
                        else
                            newset.Description=newset.Description.Replace(" ","%20");

                        myFlickr.CreatePhotoSet(newset);
                        miCancel.Enabled = false;
                        miDeletePhotoset.Enabled = true;
                        miEditPhotoset.Enabled = true;
                    }
                    else
                        MessageBox.Show("You must select one primary photo!", "Primary photo missing",
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    break;
                case PhotoSetManagerState.Editing:
                    break;
                case PhotoSetManagerState.Removing:
                    break;
                default:
                    break;
            }


            this.ResumeLayout();
        }

        private void miCancel_Click(object sender, EventArgs e)
        {
            switch (State)
            {
                case PhotoSetManagerState.BrowsingSets:
                    break;
                case PhotoSetManagerState.AddingNew:
                    this.SuspendLayout();
                    tbTitle.Text = "";
                    _titleLabel = true;
                    tbDescription.Text = "";
                    _descLabel = true;
                    lvPhotos.Items.Clear();
                    pnNewSet.Visible = !pnNewSet.Visible;
                    pnPhotosetList.Visible = !pnPhotosetList.Visible;
                    miCancel.Enabled = false;
                    miDeletePhotoset.Enabled = true;
                    miEditPhotoset.Enabled = true;
                    miNewPhotoset.Text = "New";
                    State = PhotoSetManagerState.BrowsingSets;
                    this.ResumeLayout();
                    break;
                case PhotoSetManagerState.Editing:
                    break;
                case PhotoSetManagerState.Removing:
                    break;
                default:
                    break;
            }
        }

        private void miDeletePhotoset_Click(object sender, EventArgs e)
        {
            if (lvPhotoSets.SelectedIndices.Count > 0)
            {
                myFlickr.DeletePhotoSet(
                    lvPhotoSets.Items[lvPhotoSets.SelectedIndices[0]].SubItems[2].Text);
            }
        }
    }
}